//
//  AddBankBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/22.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol AddBankBillViewDelegate {
    @objc optional func selectBankBill(title:String) //选择银行账单
}

class AddBankBillView: UIView {
    
    weak var delegate : AddBankBillViewDelegate?
    
    fileprivate let cellId = "bankBillCell"
    fileprivate let bankArray = [CITICBank,PufaBank,GuangfaBank,EverbrightBank,PeaceBank,HSBC,GuangzhouBank,MinshengBank,CitiBank,ZheshangBank,ChinaMerchantsBank,BankofCommunications,IndustrialBank,ShanghaiBank,BankofChina,ABC,ICBC,Constructionbank,PostalBank,BaoshangBank,BankofBeijing,ChongqingBank,HSBC,StandardCharteredBank]

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let redView = UIView()
        redView.layer.cornerRadius = RATIO(maxNum: 2)
        redView.layer.masksToBounds = true
        redView.backgroundColor = UIColor.red
        self.addSubview(redView)
        
        let titleLabel = UILabel()
        titleLabel.text = importBankBill
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 20))
        self.addSubview(titleLabel)
        
        let contentLabel = UILabel()
        contentLabel.text = noFurtherInformation
        contentLabel.textColor = UIColor.gray
        contentLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(contentLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.addSubview(lineView)
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = .init(width: ScreenW/4, height: RATIO_H(maxNum: 100))
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = RATIO_H(maxNum: 20)
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(AddBankBillCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        redView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.width.equalTo(RATIO(maxNum: 4))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(redView)
            make.height.equalTo(RATIO_H(maxNum: 30))
            make.width.greaterThanOrEqualTo(10)
            make.left.equalTo(redView.snp.right).offset(RATIO(maxNum: 10))
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(RATIO_H(maxNum: 20))
            make.width.greaterThanOrEqualTo(10)
            make.left.equalTo(titleLabel)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(contentLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.right.bottom.equalToSuperview()
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}

extension AddBankBillView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return bankArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? AddBankBillCell
        cell?.titleLabel.text = bankArray[indexPath.row]
        cell?.bankImageView.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: bankArray[indexPath.row]))
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectBankBill!(title: bankArray[indexPath.row])
    }
}
